package com.huanong.it.xwcommon.utils;

import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.aigc.generation.models.QwenParam;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class AliYun {
    public static String chat(String question) throws NoApiKeyException, InputRequiredException {
        List<Message> l=new ArrayList<>();
        String prompt = "请结合上下文，审核该文章及标题是否包含敏感和非法的词句，自动更改该微博的状态:如果确定非法，输出-1，如果判定为可能非法，输出3，如果正常，输出1。注意：即使文章没有提供具体的文章和标题内容，" +
                "你也只能输出:-1,1,3,不要输出任何多余的语句或者是说明" ;
        Message sysMessage=Message.builder()
                .role(Role.SYSTEM.getValue())
                .content(prompt)
                .build();
        Message userMsg = Message.builder()
                .role(Role.USER.getValue())
                .content(question).build();
        l.add(userMsg);
        l.add(sysMessage);
        Generation gen = new Generation();

        GenerationParam params = QwenParam.builder().model(Generation.Models.QWEN_MAX)
                //.prompt(prompt)
                .seed(1234)
                .topP(0.8)
                .resultFormat("message")
                .enableSearch(false)
                .maxTokens(1500)
                .temperature((float)0.85)
                .repetitionPenalty((float)1.0)
                .build();

        params.setMessages(l);
        GenerationResult result = gen.call(params);

        String ans=result.getOutput().getChoices().get(0).getMessage().getContent();

        System.out.println(ans);
        return ans;
    }
}
